package cn.lcc.controller;

import cn.lcc.domain.Order;
import cn.lcc.domain.User;
import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")

public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "userFallBack")
    public Order get(@PathVariable("id") Long id){
        /*
            发起远程调用user-server 微服务 获取一个user对象
                发送http请求调用：http://localhost:10200/user/1

            问题
                String url = "http://localhost:10200/user/1"; 用户服务做了集群，但是目前的写法只能访问一台机器

         */
        String url = "http://user-server/user/1";
        User user = restTemplate.getForObject(url, User.class);

        return new Order(id,"111111111",user);
    }
    public Order userFallBack(@PathVariable("id") Long id){
        return new Order(id,"服务不可用，请稍后再试",null);
    }
}
